#include "kshoneycombbtn.h"

KSHoneyCombBtn::KSHoneyCombBtn(QWidget *parent /*= 0*/) : QLabel(parent), m_btnstate(BTNNORMAL), m_oldbtnstate(BTNNORMAL)
{
	setMouseTracking(true);
	//SetTextAlign()
	setAlignment(Qt::AlignCenter);
}

KSHoneyCombBtn::~KSHoneyCombBtn()
{

}

void KSHoneyCombBtn::setPixmap(const QPixmap &pixmap)
{
	int w = pixmap.width() / 4;
	int h = pixmap.height();
	m_normalimg = pixmap.copy(QRect(0, 0, w, h));
	m_hoverimg = pixmap.copy(QRect(w, 0, w, h));
	m_pressedimg = pixmap.copy(QRect(2 * w, 0, w, h));
	m_disableimg = pixmap.copy(QRect(3 * w, 0, w, h));
	repaint();
}

void KSHoneyCombBtn::setDisabled(bool b)
{
	if (b)
	{
		m_btnstate = BTNDISABLE;
		BTNUPDATE;

	}
	else
	{
		m_btnstate = BTNNORMAL;
		BTNUPDATE;
	}
}

void KSHoneyCombBtn::paintEvent(QPaintEvent *e)
{
	QPainter painter;
	painter.begin(this);
	switch (m_btnstate)
	{
	case KSHoneyCombBtn::BTNNORMAL:
		painter.drawPixmap(rect(), m_normalimg);
		break;
	case KSHoneyCombBtn::BTNHOVER:
		painter.drawPixmap(rect(), m_hoverimg);
		break;
	case KSHoneyCombBtn::BTNPRESS:
		painter.drawPixmap(rect(), m_pressedimg);
		break;
	case KSHoneyCombBtn::BTNDISABLE:
		painter.drawPixmap(rect(), m_hoverimg);
		break;
	default:
		break;
	}
	//painter.drawPixmap(rect(), m_normalimg);
	painter.end();

	QLabel::paintEvent(e);
}

void KSHoneyCombBtn::leaveEvent(QEvent *event)
{
	m_btnstate = BTNNORMAL;
	BTNUPDATE;

	QLabel::leaveEvent(event);
}

void KSHoneyCombBtn::mouseMoveEvent(QMouseEvent *e)
{
	m_isActive = isInRect(e->pos());
	if (m_isActive)
	{
		m_btnstate = BTNHOVER;
		BTNUPDATE;

	}
	else
	{
		m_btnstate = BTNNORMAL;
		BTNUPDATE;
	}
	QLabel::mouseMoveEvent(e);
}

void KSHoneyCombBtn::mousePressEvent(QMouseEvent *e)
{
	m_isActive = isInRect(e->pos());
	if (m_isActive)
	{
		m_btnstate = BTNPRESS;
		BTNUPDATE;
		emit clicked();
	}
	else
	{

	}
	QLabel::mousePressEvent(e);
}

bool KSHoneyCombBtn::isInRect(QPoint p)
{
	QRect r = this->rect();
	if (p.x() > 0.25 * r.width() && p.x() < 0.75 * r.width())
	{
		return true;
	}
	else
	{
		return false;
	}
}




